package com.shd.edanpei.presenter;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.ui.activity.HouseNeedDataActivity2;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.presenter.contract.HouseNeedDataContract;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/9/23.
 * yang-wen-wen
 */

public class HouseNeedDataPresenter extends BasePresenterImpl<HouseNeedDataContract.HouseNeedDataIView>
        implements HouseNeedDataContract.HouseNeedDataIPresenter {

    private CustomerBean bean = new CustomerBean();
    private Bundle bundle = new Bundle();

    public HouseNeedDataPresenter(HouseNeedDataContract.HouseNeedDataIView mView) {
        super(mView);
    }

    @Override
    public void setCustomerBean(CustomerBean bean) {
        if (bean != null) {
            this.bean = bean;
            mBaseIView.setData(bean);
        }
    }


    @Override
    public void getOrderInfo(final int orderID) {
        ApiCreator.getApi().getOrderInfo(orderID)
                .compose(RxTransformer.<CustomerBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<CustomerBean>() {
                    @Override
                    protected void onSuccess(CustomerBean data) {
                        bean.setOrderId(orderID + "");
                        bean = data;
                        mBaseIView.setData(data);
                    }
                });
    }

    @Override
    public void setHouseSize(String str) {
        bean.setRoomSize(str);
    }

    @Override
    public void setHouseNature(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "住房性质", R.array.house_nature, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setRoomType(data);
            }
        });
    }

    @Override
    public void setHouseAge(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "房龄", R.array.house_age, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setRoomAge(data);
            }
        });
    }

    @Override
    public void setHouseStatus(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "住房现状", R.array.house_status, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setRoomStatus(data);


            }
        });
    }

    @Override
    public void setHouseAddress(final TextView text) {
        DialogUtil.addressPick(mBaseIView.getContext(), new DialogUtil.AddressListener() {

            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                mBaseIView.setText(text, address);
                bean.setRoomArea(address);
            }
        });
    }

    @Override
    public void setDetailedAddress(String str) {
        bean.setHouseDetailAddress(str);
    }

    @Override
    public void setAmount(final TextView text) {
        DialogUtil.moneyLimitDialog(mBaseIView.getContext(), "订单金额", Constant.Unit.WANYUAN.getUnit(), new DialogUtil.Listener() {

            @Override
            public void done(String data) {
                mBaseIView.setText(text, data + Constant.Unit.WANYUAN.getUnit());
                bean.setApplyAmount(Integer.parseInt(data));
            }
        });
    }

    @Override
    public void setRate(final TextView text) {
        DialogUtil.rateListDialog(mBaseIView.getContext(), new DialogUtil.RateListener() {
            @Override
            public void done(String str, String min, String max) {
                mBaseIView.setText(text, str);
                bean.setApplyInterest(str);
            }
        });
    }

    @Override
    public void setRepayWay(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "归还方式", R.array.repay_way, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                bean.setApplyRepaymentType(data);
                mBaseIView.setText(text, data);
            }
        });
    }


    @Override
    public void setRepayDate(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "订单期限", R.array.repay_date, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setApplyTerm(data);
            }
        });
    }

    @Override
    public void setOtherAsset(boolean b) {
        bean.setHaveOtherHouse(b);
    }

    @Override
    public void setCompany(boolean b) {
        bean.setHaveCompany(b);
    }

    @Override
    public void setCompanyStake(String str) {
        bean.setCompanyShare(str);
    }

    @Override
    public void setOperationState(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "经营状况", R.array.operation_state, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setOperationState(data);
            }
        });
    }

    @Override
    public void setOperationTerm(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "经营年限", R.array.operation_term, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setOperationTerm(data);
            }
        });
    }
//
//    @Override
//    public void setSite(boolean b) {
//        bean.setHaveField(b);
//    }
//
//
//    @Override
//    public void setSiteSize(String str) {
//        bean.setFieldArea(str);
//    }

    @Override
    public void setRemark(String str) {
        bean.setRemark(str);
    }

    @Override
    public void next(int from) {
        if (checkParams1()) {
            bundle.clear();
            bundle.putParcelable(Constant.DATA, bean);
            bundle.putInt(Constant.FROM, from);
            mBaseIView.startActivity(HouseNeedDataActivity2.class, bundle);
        }
    }


    @Override
    public void done() {
        Map<String, Object> map = checkParams2();
        if (map != null && !map.isEmpty()) {
            mBaseIView.showLoading("");
            ApiCreator.getApi().submitHouseOrder(map)
                    .compose(RxTransformer.<Integer>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<Integer>() {
                        @Override
                        protected void onSuccess(Integer data) {
                            bean.setOrderId(data + "");
                            RxBus.get().send(RxBusCode.NEED_DATA_SUBMIT_SUCCESS, bean);
                            DialogUtil.infoDialog2(mBaseIView.getContext(),
                                    "提示",
                                    "资料提交成功，请前去完成第3步\"完善资料\"，方可发单成功",
                                    "去完善资料",
                                    new DialogUtil.SureListener() {
                                        @Override
                                        public void sure() {
                                            mBaseIView.getContext().finish();
                                        }
                                    });
                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });
        }
    }

    @Override
    public void update() {
        Map<String, Object> map = checkParams2();
        if (map != null && !map.isEmpty()) {
            mBaseIView.showLoading("正在修改...");
            ApiCreator.getApi().updataHouseData(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            ToastUtil.showToast("修改成功");
                            RxBus.get().send(RxBusCode.NEED_DATA_SUBMIT_SUCCESS, bean);
                            RxBus.get().send(RxBusCode.other,bean.isHaveOtherHouse());
                            RxBus.get().send(RxBusCode.company,bean.isHaveCompany());
                            mBaseIView.getContext().finish();
                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });
        }
    }


    /**
     * 验证住房所需材料 上
     */
    private boolean checkParams1() {
        if (isNull(bean.getRoomSize(), "请填写住房面积")) return false;
        if (isNull(bean.getRoomType(), "请选择住房性质")) return false;
        if (isNull(bean.getRoomAge(), "请选择房龄")) return false;
        if (isNull(bean.getRoomStatus(), "请选择住房现状")) return false;
        if (isNull(bean.getRoomArea(), "请选择住房所在地")) return false;
        if (isNull(bean.getHouseDetailAddress(), "请填写详细地址")) return false;
        return true;
    }

    /**
     * 验证住房所需材料 下
     */
    private Map<String, Object> checkParams2() {
        if (isNull(bean.getApplyAmount(), "请选择订单金额")) return null;
        if (isNull(bean.getApplyInterest(), "请选择预期比")) return null;
        if (isNull(bean.getApplyRepaymentType(), "请选择归还方式")) return null;
        if (isNull(bean.getApplyTerm(), "请选择订单期限")) return null;
        if (bean.isHaveCompany() && isNull(bean.getCompanyShare(), "请填写占股比例")) return null;
        if (bean.isHaveCompany() && isNull(bean.getOperationState(), "请选择经营状况")) return null;
        if (bean.isHaveCompany() && isNull(bean.getOperationTerm(), "请选择经营年限")) return null;
//        if (bean.isHaveField() && isNull(bean.getFieldArea(), "请填写场地面积")) return null;
        return GsonUtil.toMap(bean);
    }

    /**
     * 验空
     */
    private boolean isNull(String str, String toast) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.showToast(toast);
            return true;
        }
        return false;
    }

}
